import {createRouter, createWebHistory} from 'vue-router'
import {unauthorized} from "@/net/index.js";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'welcome',
            component: () => import('@/views/WelcomeView.vue'),
            children: [
                {
                    path: '',
                    name: 'welcome-login',
                    component: () => import('@/views/Welcome/Login.vue')
                },
                {
                    path: 'register',
                    name: 'welcome-register',
                    component: () => import('@/views/Welcome/Register.vue')
                },
                {
                    path: 'reset',
                    name: 'welcome-reset',
                    component: () => import('@/views/Welcome/Reset.vue')
                }
            ]
        },
        {
            path: '/index',
            name: 'index',
            component: () => import('@/views/index/index.vue'),
            redirect: '/index/home',
            children: [
                {
                    path: 'home',
                    name: 'home',
                    component: () => import('@/views/index/Content.vue')
                },
                {
                    path: 'Collection',
                    name: 'Collection',
                    component: () => import('@/views/Collection/Collection.vue')
                },
                {
                    path: 'CodeTool',
                    name: 'CodeTool',
                    component: () => import('@/views/CodeTool/CodeTool.vue'),
                    children: [
                        {
                            path: 'MD5',
                            name: 'MD5',
                            component: () => import('@/views/CodeTool/SpecificFunction/MD5.vue'),
                            meta: {hideParentContent: true}
                        },
                        {
                            path: 'number_change',
                            name: 'CodeTool_number_change',
                            component: () => import('@/views/CodeTool/SpecificFunction/NumberChanger.vue'),
                            meta: {hideParentContent: true}
                        }
                    ]
                },
                {
                    path: 'LifeTool',
                    name: 'LifeTool',
                    component: () => import('@/views/LifeTool/LifeTool.vue'),
                    children: [
                        {
                            path: 'relationship',
                            name: 'relationship',
                            component: () => import('@/views/LifeTool/SpecificFunction/Relationship.vue'),
                            meta: {hideParentContent: true}
                        },
                        {
                            path: 'BMI',
                            name: 'BMI',
                            component: () => import('@/views/LifeTool/SpecificFunction/BMI.vue'),
                            meta: {hideParentContent: true}
                        },
                        {
                            path: 'Date',
                            name: 'Date',
                            component: () => import('@/views/LifeTool/SpecificFunction/Date.vue'),
                            meta: {hideParentContent: true}
                        }
                    ]
                },
                {
                    path: 'TextTool',
                    name: 'TextTool',
                    component: () => import('@/views/TextTool/TextTool.vue'),
                    children: [
                        {
                            path: 'letter_change',
                            name: 'letter_change',
                            component: () => import('@/views/TextTool/SpecificFunction/LetterChange.vue'),
                            meta: {hideParentContent: true}
                        },
                        {
                            path: 'Count',
                            name: 'Count',
                            component: () => import('@/views/TextTool/SpecificFunction/Count.vue'),
                            meta: {hideParentContent: true}
                        }
                    ]
                },
                {
                    path: 'MathTool',
                    name: 'MathTool',
                    component: () => import('@/views/MathTool/MathTool.vue'),
                    children: [
                        {
                            path: 'number_change',
                            name: 'MathTool_number_change',
                            component: () => import('@/views/MathTool/SpecificFunction/NumberChanger.vue'),
                            meta: {hideParentContent: true}
                        },
                        {
                            path: 'area_change',
                            name: 'area_change',
                            component: () => import('@/views/MathTool/SpecificFunction/AreaChanger.vue'),
                            meta: {hideParentContent: true}
                        },
                        {
                            path: 'byte_change',
                            name: 'byte_change',
                            component: () => import('@/views/MathTool/SpecificFunction/ByteChanger.vue'),
                            meta: {hideParentContent: true}
                        },
                        {
                            path: 'length_change',
                            name: 'length_change',
                            component: () => import('@/views/MathTool/SpecificFunction/LengthChanger.vue'),
                            meta: {hideParentContent: true}
                        },
                        {
                            path: 'quality_change',
                            name: 'quality_change',
                            component: () => import('@/views/MathTool/SpecificFunction/QualityChange.vue'),
                            meta: {hideParentContent: true}
                        },
                        {
                            path: 'time_change',
                            name: 'time_change',
                            component: () => import('@/views/MathTool/SpecificFunction/TimeChange.vue'),
                            meta: {hideParentContent: true}
                        }
                    ]
                }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    const isUnauthorized = unauthorized()

    const isWelcomeRoute = typeof to.name === 'string' && to.name.startsWith("welcome-")
    const isIndexRoute = typeof to.fullPath === 'string' && to.fullPath.startsWith('/index')

    if (isWelcomeRoute && !isUnauthorized) {
        next('/index')
    } else if (isIndexRoute && isUnauthorized) {
        next("/")
    } else {
        next()
    }
})

export default router